package com.dailyblue.java.spring.boot.redis.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/hash")
@Slf4j
public class HashController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/a")
    public String a() {
        HashOperations<String, String, String> hash = stringRedisTemplate.opsForHash();
        hash.put("A","A","aaa");
        hash.put("A","B","bbb");
        hash.put("A","C","ccc");
        hash.put("A","D","ddd");
        hash.putIfAbsent("A","A","ddd");
        Map<String,String> map = new HashMap<>();
        map.put("M1","mmm1");
        map.put("M2","mmm2");
        map.put("M3","mmm3");
        map.put("M4","mmm4");
        hash.putAll("A",map);
        return "";
    }
    @GetMapping("/b")
    public String b(){
        HashOperations<String, String, String> hash = stringRedisTemplate.opsForHash();
        String v1 = hash.get("A","B");
        Map<String,String> map = hash.entries("A");
        Set<String> keys = hash.keys("A");
        Collection<String> values = hash.values("A");
        String randomKey = hash.randomKey("A");
        List<String> list = hash.randomKeys("A",3);
        log.info("获取到B的值:{}",v1);
        log.info("获取到所有的内容:{}",map);
        log.info("获取到所有的Key:{}",keys);
        log.info("获取到所有的值:{}",values);
        log.info("随机获取到的Key:{}",randomKey);
        log.info("随机获取到的一群key:{}",list);
        return null;
    }
    @GetMapping("/c")
    public String c(){
        HashOperations<String, String, String> hash = stringRedisTemplate.opsForHash();
        hash.delete("A","A","M2");
        long len = hash.size("A");
        log.info("集合的长度是：{}",len);
        return null;
    }
}
